﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    
    /// <summary>
    /// Removes spur pixels. For example:
    /// These are pixels that are connected only diagonally to other pixels and connected in only one direction:
    ///0  0  0  0           0  0  0  0
    ///0  0  0  0           0  0  0  0
    ///0  0  1  0  becomes  0  0  0  0
    ///0  1  0  0           0  1  0  0
    ///1  1  0  0           1  1  0  0
    /// </summary>
    /// <remarks>See http://www.johnloomis.org/ece563/notes/BinaryImages/morph/morph.html to see what result we should get</remarks>
    public class Spur : BaseForeBackgroundColorFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Spur"/> class.
        /// </summary>
        public Spur()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Spur"/> class.
        /// </summary>
        /// <param name="_spur">The _spur.</param>
        internal Spur(Spur _spur)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int y = 0;
            int x = 0;
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            Color[] aColor = new Color[8];

            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();

            int nForegroundCount = 0;
            int nBackgroundCount = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    aColor = Segmentation.GetEnvironmentPixelColorArray(bitmapCloned, x, y, Definitions.EnvironmentPointListClockwise, aColor);

                    nForegroundCount = 0;

                    nForegroundCount = aColor[0] == m_ColorForeground ? nForegroundCount++ : nForegroundCount;
                    nForegroundCount = aColor[4] == m_ColorForeground ? nForegroundCount++ : nForegroundCount;
                    nForegroundCount = aColor[6] == m_ColorForeground ? nForegroundCount++ : nForegroundCount;
                    nForegroundCount = aColor[0] == m_ColorForeground ? nForegroundCount++ : nForegroundCount;
                    nBackgroundCount = aColor[1] == m_ColorBackground ? nBackgroundCount++ : nForegroundCount;
                    nBackgroundCount = aColor[3] == m_ColorBackground ? nBackgroundCount++ : nForegroundCount;
                    nBackgroundCount = aColor[5] == m_ColorBackground ? nBackgroundCount++ : nForegroundCount;
                    nBackgroundCount = aColor[7] == m_ColorBackground ? nBackgroundCount++ : nForegroundCount;
                    if (nForegroundCount == 1 && nBackgroundCount == 4)
                    {
                        _bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            bitmapCloned.Dispose();
            return _bitmap;
        }

        public override object Clone()
        {
            return new Spur(this);
        }
    }
}
